<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *售后列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'用户列表'),
);
$batch=array(
	array('type'=>'api','url'=>'/yonghu.danganlist/export/','title'=>'导出售后档案')
);
//售后列表
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('fromstoreid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']!='all' && $_RQ['status']){
		$params['status']=intval($_RQ['status']);
	}
	if($_RQ['classid']){
		$params['classid']=intval($_RQ['classid']);
		$return['params']['classid']=$_RQ['classid'];
	}
	$return['params']['status']=$_RQ['status'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_yonghu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('yonghu')->member_show($item['id']);
			if($item['danganimage1'] && $item['danganimage2']){
				$item['isdangan']=1;
			}
			if(!$item['danganimage1'] || !$item['danganimage2']){
				$item['isdangan']=2;
			}
			if(!$item['danganimage1'] && !$item['danganimage2']){
				$item['isdangan']=0;
			}
			$item['createtime']=date('Y-m-d',$item['createtime']);
			$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item=array_merge($member,$item);
			$infee_log=pdo_get('jy_yxw_infee_log',array('memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item['pay_type']=$infee_log['pay_type'];
			$item['fee']=$infee_log['fee'];
			$item['worker6']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker6id']),'title');
			$item['set']='/yonghu.danganlist/set/id='.$item['id'];
			$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_message'=>'确认删除该售后吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'storetitle','title'=>'售后基地','type'=>'string'),
			array('name'=>'classid','title'=>'售后群','type'=>'select','options'=>'class'),
			array('type'=>'string','title'=>'身份证','name'=>'cardno'),
			array('type'=>'radio','title'=>'学历','name'=>'xueli','options'=>'xueli_type'),
			array('type'=>'string','title'=>'单费','name'=>'fee'),
			array('type'=>'radio','title'=>'缴费方式','name'=>'pay_type','options'=>'pay_types'),
			array('name'=>'worker6','title'=>'销售员员','type'=>'string'),
			array('name'=>'isdangan','title'=>'上传情况','type'=>'radio','options'=>'isdangan'),
			array('name'=>'status','title'=>'状态','type'=>'radiomodal','options'=>'status','action'=>'set'),
			array('name'=>'createtime','title'=>'成交日期','type'=>'string'),
		),'action'=>array(
			//array('type'=>'href','action'=>'/yonghu.danganlist/danganimage/','title'=>'上传档案照片'),
			array('type'=>'href','action'=>'/yonghu.danganlist/lookdanganinfo/','title'=>'查看档案资料'),
			//array('type'=>'api','url'=>'/yonghu.danganlist/uppdf01/','title'=>'导出档案模板'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'售后名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'classid','width'=>'15','title'=>'售后群','type'=>'select','options'=>'class'),
			//array('name'=>'storeid','width'=>'15','title'=>'所属门店','type'=>'select','options'=>'stores'),
		),
	);
	//$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['class']=$this->getall('class',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	//$return['workers']=$this->getall('worker',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['status']=$this->yonghu_status;
	$return['jibie_type']=$this->member_jibie_type;
	$return['xueli_type']=$this->xueli_type;
	$return['pay_types']=$this->pay_types;
	$return['isdangan']=array('未上传档案','已上传档案','已上传部分档案');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='danganimage'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if(!$post['danganimage1'] || !$post['danganimage2']){
			exi('请上传档案照片A面和B面！','error');
		}
		pdo_update('jy_yxw_yonghu',array('danganimage1'=>$post['danganimage1'],'danganimage2'=>$post['danganimage2'],),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','/yonghu.danganlist//');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_yonghu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'上传档案照片',
	);
	$htmls['edit']=array(
		array('type'=>'image','title'=>'上传档案照片A面','name'=>'danganimage1'),
		array('type'=>'image','title'=>'上传档案照片B面','name'=>'danganimage2'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='lookdanganimage'){
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_yonghu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'查看档案照片',
	);
	$htmls['look']=array(
		array('type'=>'image','title'=>'上传档案照片A面','name'=>'danganimage1'),
		array('type'=>'image','title'=>'上传档案照片B面','name'=>'danganimage2'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='lookdanganinfo'){
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_yonghu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['createtime']=date('Y-m-d',$item['createtime']);
		$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
		$item=array_merge($member,$item);
		$infee_log=pdo_get('jy_yxw_infee_log',array('memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
		$item['pay_type']=$infee_log['pay_type'];
		$item['fee']=$infee_log['fee'];
		$item['worker6']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker6id']),'title');
	}else{
		exi('请先选择售后','error');
	}
	$item['wj1']=pdo_count('jy_yxw_weiji_log',array('type'=>1,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['wj2']=pdo_count('jy_yxw_weiji_log',array('type'=>2,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['wj3']=pdo_count('jy_yxw_weiji_log',array('type'=>3,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['goodnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>0,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['cdnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>1,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['bjnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>2,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['sjnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>3,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$item['kknum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>4,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$htmls=array(
		'type'=>'look',
	);
	$htmls['look']=array(
			array('name'=>'title','title'=>'个人资料','type'=>'help','help'=>''),
			array('name'=>'title','title'=>'售后姓名','type'=>'string'),
			array('type'=>'string','title'=>'身份证','name'=>'cardno'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'logo','title'=>'相片','type'=>'image'),
			array('name'=>'title','title'=>'个人档案','type'=>'help','help'=>''),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'classid','title'=>'售后群','type'=>'select','options'=>'class'),
			array('name'=>'createtime','title'=>'成交日期','type'=>'string'),
			array('type'=>'radio','title'=>'学历','name'=>'xueli','options'=>'xueli_type'),
			array('type'=>'string','title'=>'单费','name'=>'fee'),
			array('type'=>'radio','title'=>'缴费方式','name'=>'pay_type','options'=>'pay_types'),
			array('name'=>'worker6','title'=>'销售员员','type'=>'string'),
			array('type'=>'image','title'=>'上传档案照片A面','name'=>'danganimage1'),
			array('type'=>'image','title'=>'上传档案照片B面','name'=>'danganimage2'),
			array('name'=>'title','title'=>'学习情况','type'=>'help','help'=>''),
			array('type'=>'string','title'=>'普通违纪次数','name'=>'wj1'),
			array('type'=>'string','title'=>'中等违纪次数','name'=>'wj2'),
			array('type'=>'string','title'=>'严重违纪次数','name'=>'wj3'),
			array('type'=>'string','title'=>'正常考勤天数','name'=>'goodnum'),
			array('type'=>'string','title'=>'迟到次数','name'=>'cdnum'),
			array('type'=>'string','title'=>'病假次数','name'=>'bjnum'),
			array('type'=>'string','title'=>'事假次数','name'=>'sjnum'),
			array('type'=>'string','title'=>'旷课次数','name'=>'kknum'),
	);
	$fangtans=$this->getall('fangtan_log',array('yonghuid'=>$item['id'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
	$htmls['look'][]=array('name'=>'title','title'=>'访谈情况','type'=>'help','help'=>'');
	$fangtan_type=$this->fangtan_type;
	foreach($fangtans as $k=>$fv){
		$title=date('Y-m-d',$fv['createtime']).$fangtan_type[$fv['type']].'情况';
		$item['ftmsg'.$k]=$fv['remark'];
		$htmls['look'][]=array('type'=>'string_log','title'=>$title,'name'=>'ftmsg'.$k);
	}
	$fankuis=$this->getall('fankui_log',array('yonghuid'=>$item['id'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
	$htmls['look'][]=array('name'=>'title','title'=>'反馈情况','type'=>'help','help'=>'');
	foreach($fankuis as $k=>$jv){
		if(!$jv['fankuitype']){
			$title=date('Y-m-d',$jv['createtime']).$fankui_type[$jv['fankuitype']].'情况';
			$msg='累计面试['.$jv['miannum'].']次,累计拿到offer['.$jv['offernum'].']次。';
		}else{
			$title=$jv['fankuitime'].'反馈';
			$msg='公司['.$jv['fankuigs'].'],反馈岗位['.$jv['fankuigw'].'],试用期薪资['.$jv['fankuifee1'].'],转正薪资['.$jv['fankuifee2'].']';
		}
		$item['fankuimsg'.$k]=$msg;
		$htmls['look'][]=array('type'=>'string_log','title'=>$title,'name'=>'fankuimsg'.$k);
	}
	$return['class']=$this->getall('class',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['status']=$this->yonghu_status;
	$return['jibie_type']=$this->member_jibie_type;
	$return['xueli_type']=$this->xueli_type;
	$return['pay_types']=$this->pay_types;
	$return['item']=$item;	
	$return['isdangan']=array('未上传档案','已上传档案','已上传部分档案');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('status'=>$post['status']);
		pdo_update('jy_yxw_yonghu',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','/yonghu.danganlist//status=all');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_yonghu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'设置状态','name'=>'status','options'=>'status'),
	);
	$return['status']=$this->yonghu_status;
	unset($return['status'][101],$return['status'][102],$return['status'][103],$return['status'][104],$return['status'][105],$return['status'][106]);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_yonghu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回售后列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'售后姓名','name'=>'title','help'=>''),
					array('type'=>'string','title'=>'售后电话','name'=>'mobile','help'=>''),
					array('type'=>'string','title'=>'紧急联系人电话','name'=>'mobile2','help'=>''),
					array('type'=>'image','title'=>'售后照片','name'=>'logo','help'=>''),
					array('type'=>'text','title'=>'售后描述','name'=>'desc','help'=>''),			
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'mobile'=>trim($postdata['mobile']),
		'mobile2'=>trim($postdata['mobile2']),
		'logo'=>trim($postdata['logo']),
		'desc'=>trim($postdata['desc']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_yxw_yonghu',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['storeid']=$_SESSION['storeid'];
		pdo_insert('jy_yxw_yonghu',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	$filename='candel/'.random(15);
	@mkdir(ROOT_AT.'candel/');
	@mkdir(ROOT_AT.$filename);
	$readme='[温馨提示]：文件分两部分，一个是售后列表，一个是售后档案图片文件夹！';
	file_put_contents(ROOT_AT.$filename.'/readme.txt',$readme);
	//售后列表
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']!='all' && $_RQ['status']){
		$params['status']=intval($_RQ['status']);
	}
	if($_RQ['classid']){
		$params['classid']=intval($_RQ['classid']);
		$return['params']['classid']=$_RQ['classid'];
	}
	$return['params']['status']=$_RQ['status'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['ids']){
		$ids=trim($_RQ['ids'],';');
		$ids=explode(';',$ids);
		$params['id']=$ids;
	}
	$list=pdo_getall('jy_yxw_yonghu',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['status']=$this->yonghu_status[$item['status']];
			$item['createtime']=date('Y/m/d',$item['createtime']);
			$this->model('yonghu')->dangandown($item,$filename);
			$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item=array_merge($member,$item);
			$item['xueli']=$this->xueli_type[$item['xueli']];
			$infee_log=pdo_get('jy_yxw_infee_log',array('memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item['pay_type']=$this->pay_types[$infee_log['pay_type']];
			$item['fee']=$infee_log['fee'];
			$item['worker6']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker6id']),'title');
			$item['classid']=pdo_getcolumn('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classid']),'title');
		}
	}
	$header=array(
		'id'=>'售后ID',
		'title'=>'售后姓名',
		'mobile'=>'手机号',
		'mobile2'=>'手机号',
		'cardno'=>'身份证号',
		'sn'=>'编号',
		'classid'=>'售后群',
		'xueli'=>'学历',
		'fee'=>'单费',
		'pay_type'=>'缴费方式',
		'worker6'=>'销售员员',
		'status'=>'售后状态',
		'createtime'=>'成交日期',
	);
	model('core/tools')->export2excel($header,$list,'售后列表','',$filename);
	$baoname='售后档案'.random(3);
	cfc('tools')->down_zip(ROOT_AT.$filename,$baoname);
}
if($_RQ['op']=='uppdf01'){
	$id=$_RQ['id'];
	$this->model('uppdf')->dangan($id);
	exit;
}
if($_RQ['op']=='del'){
	exi('不能删除！','error');	
}